package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    @Override
    public boolean save(Customer customer) {
        if (customer.getCustomerId() != null) {
            customerDao.updateCustomerById(customer);
        } else {
            customerDao.insertCustomer(customer);
        }
        return true;
    }

    @Override
    public Map<String, Object> listCustomer(Integer pageNumber, Integer pageSize, String customerName) {
        List<Customer> customerList = customerDao.selectCustomPageByNameIfPresent((pageNumber - 1) * pageSize, pageSize, customerName);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", customerList.size());
        map.put("rows", customerList);
        return map;
    }

    @Override
    public boolean deleteBatch(String... customerIds) {
        for (String customerId : customerIds) {
            customerDao.delete(customerId);
        }
        return true;
    }
}
